/*
 * @(#)UserController.java
 * UserController类作为SpringMVC的Controller层
 * 主要负责对用户方面的请求的映射处理，调用Service层的方法对请求进行处理并返回结果
 * Copyright 2015 上海领智信息科技有限公司, Inc. All rights reserved.
 * @author reeves
 * @Time 2015/04/20 21:37:00
 * @version 1.0
 */

package com.version1_2.controller;

import com.alibaba.fastjson.JSONObject;
import com.util.format.MapToJson;
import com.version1_1.controller.UserController;
import com.version1_2.service.UserService1_2;
import com.version1_3.controller.UserController1_3;
import com.version1_3.service.UserSerivce1_3;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @author reeves
 * @ClassName: UserController
 * @Description: 用户相关接口控制器
 * @date 2015年4月25日 下午4:47:52
 */
@Controller
public class UserController1_2 {

    @Autowired
    private UserController userController; // 复用1.1版本的controller层部分接口

    @Autowired
    private UserSerivce1_3 userSerivce13; // 复用1.3版本的带邀请码注册接口

    @Autowired
    private UserService1_2 userService12;

    /*创建Log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(UserController1_2.class);

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/login
     *
     * @Description: 用户登陆 OK
     */
    @RequestMapping(value = "/version1_2/user/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String login(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        String baiduId = "";
        if (jsonObject.getString("baiduId") != null) {
            baiduId = jsonObject.getString("baiduId");
        }

        // 参数错误
        if (jsonObject == null) {
            LOGGER.error("用户登陆错误,loginString为null...");
            return "{\"userId\":-1,\"errorType\":2}";
        }

        // 解析JSON参数
        return userService12.userLogin(
                jsonObject.getIntValue("userId"), jsonObject.getString("mail"),
                jsonObject.getString("telnumber"),
                jsonObject.getString("password"),baiduId);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/changePasswd
     *
     * @Description: 登陆用户更改密码 OK
     */
    @RequestMapping(value = "/version1_2/user/changePasswd", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String changePasswd(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        // 参数错误
        if (jsonObject == null) {
            LOGGER.error("登陆用户更改密码错误,updatePasswdString为null...");
            return "{\"updatePasswdResult\":false}";
        }

        // 返回密码更新结果
        return userService12.updatePasswd(jsonObject);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/updateInfo
     *
     * @Description: 更改用户个人信息
     */
    @RequestMapping(value = "/version1_2/user/updateInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String updateInfo(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        // 参数错误
        if (jsonObject == null) {
            LOGGER.error("更新用户个人信息错误,updateInfoString为null");
            return "{\"updateResult\":false}";
        }

        return userService12.updateUserInfo(jsonObject);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/updateFocusList
     *
     * @Description: 更新关注列表
     */
    @RequestMapping(value = "/version1_2/user/updateFocusList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String updateFocusList(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        // 参数错误
        if (jsonObject == null) {
            LOGGER.error("登陆用户更新关注列表错误,updateListString为null...");
            return "{\"focusUpdateResult\":false}";
        }

        // 返回用户关注列表更新结果
        return userService12.updateFocusList(jsonObject);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/feedback
     *
     * @Description: 用户反馈 device-error 连接服务器失败
     */
    @RequestMapping(value = "/version1_2/user/feedback", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String feedback(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        // 参数错误
        if (jsonObject == null) {
            LOGGER.error("用户反馈错误,客户端参数为null...");
            return "{\"code\":500}";
        }

        // 返回用户反馈结果
        return userService12.feedback(jsonObject);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/doCollect
     *
     * @Description: 记录、删除用户单次收藏 device-error 记录失败
     */
    @RequestMapping(value = "/version1_2/user/doCollect", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String doCollect(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        if (jsonObject == null) {
            LOGGER.error("记录、删除用户单次收藏错误,collectString为null...");
            return "{\"collectResult\":false}";
        }
        // 返回记录结果
        return userService12.doCollect(jsonObject);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/delCollect
     *
     * @Description: 批量删除用户收藏 device-error 删除成功,但是再次进入又有
     */
    @RequestMapping(value = "/version1_2/user/delCollect", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String delCollect(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        boolean result = true;

        if (jsonObject == null) {
            LOGGER.error("删除用户收藏错误,collectDelString为null...");
            result = false;
        } else {
            try {
                userService12.delCollect(
                        jsonObject.getIntValue("userId"),
                        jsonObject.getString("deleteList"));
            } catch (Exception exception) {
                LOGGER.error("批量删除用户收藏出错: " + exception);
                result = false;
            }
        }

        return new MapToJson("delResult", result).toJsonString();
    }


    /**
     * @Description: 登录用户得到收藏列表 OK
     */
    @RequestMapping(value = "/version1_2/user/getCollectList/{userId}/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getCollectList(@PathVariable("userId") int userId, @PathVariable("pageNumber") int pageNumber)
            throws UnsupportedEncodingException {
        return userService12.getCollectList(userId, pageNumber);
    }

    /**
     * @Description: 根据二级分类ID获取用户收藏中特定的二级分类的文章 OK
     */
    @RequestMapping(value = "/version1_2/user/getSecondTypeCollectList/{userId}/{secondType}/{pageNumber}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getSecondTypeCollectList(@PathVariable("userId") int userId, @PathVariable("secondType") int secondType, @PathVariable("pageNumber") int pageNumber) throws UnsupportedEncodingException {
        return userService12.getSecondTypeCollectList(userId, secondType, pageNumber);
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_2/user/register/{telnumber}/{password}/{inviteCode}
     *
     * @Description: 用户注册, 使用邀请码
     */
    @RequestMapping(value = "/version1_2/user/register/{telnumber}/{password:.+}/{inviteCode}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String registerwithInviteCode(@PathVariable("telnumber") String telnumber, @PathVariable("password") String password, @PathVariable("inviteCode") String inviteCode)
            throws UnsupportedEncodingException {
        return userSerivce13.registerWithInviteCode(telnumber, password, inviteCode);
    }

    /*-------------------------------------- 复用version1.1中controller层中的部分接口  ------------------------------------------*/

    /**
     * @Description: 用户注册
     */
    @RequestMapping(value = "/version1_2/user/register/{telnumber}/{password:.+}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String register(@PathVariable("telnumber") String telnumber, @PathVariable("password") String password)
            throws UnsupportedEncodingException {
        return userController.register(telnumber, password);
    }

    /**
     * @Description: 检查是否有消息 device-error 没出错,但是没有红点
     */
    @RequestMapping(value = "/version1_2/user/message/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String hasMessage(@PathVariable("userId") int userId) {
        return userController.hasMessage(userId);
    }

    /**
     * @Description: 获取用户回复列表信息 OK
     */
    @RequestMapping(value = "/version1_2/user/message/reply/{userId}/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getReplyMessage(@PathVariable("userId") int userId,
                                  @PathVariable("pageNumber") int pageNumber)
            throws UnsupportedEncodingException {
        return userController.getReplyMessage(userId, pageNumber);
    }

    /**
     * @Description: 获取用户点赞列表信息 OK
     */
    @RequestMapping(value = "/version1_2/user/message/praise/{userId}/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getPraiseMessage(@PathVariable("userId") int userId,
                                   @PathVariable("pageNumber") int pageNumber)
            throws UnsupportedEncodingException {
        return userController.getPraiseMessage(userId, pageNumber);
    }

    /**
     * @Description: 获取用户系统消息, 正在写，需要设计方面给出设计。。。
     */
    @RequestMapping(value = "/version1_2/user/message/system/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getSystemMessage(@PathVariable("userId") int userId)
            throws UnsupportedEncodingException {
        return userController.getSystemMessage(userId);
    }

    /**
     * @Description: 验证用户邮箱或手机号已经被注册 device-no result 注册页面无法得到验证码
     */
    @RequestMapping(value = "/version1_2/user/checkRegister/{registerInfo}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String checkRegister(@PathVariable("registerInfo") String registerInfo) {
        return userController.checkRegister(registerInfo);
    }

    /**
     * @description: 忘记密码 OK
     */
    @RequestMapping(value = "/version1_2/user/forgetPass/{telnumber}/{newPassword:.+}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String forgetPass(@PathVariable("telnumber") String telnumber, @PathVariable("newPassword") String newPassword) {
        return userController.forgetPass(telnumber, newPassword);
    }


}
